package com.platform.qujia.controller;


import com.alibaba.fastjson.JSONObject;
import com.platform.qujia.constantEnum.TianQuetechPayEnum;
import com.platform.qujia.pojo.TradeReversePayVO;
import com.platform.qujia.pojo.dto.DualNonOilOrderPayListDto;
import com.platform.qujia.pojo.dto.ScanOrderDTO;
import com.platform.qujia.pojo.nonOil.NonOilOrder;
import com.platform.qujia.service.DualNonOilOrderPayService;
import com.platform.qujia.utils.CodeMsg;
import com.platform.qujia.utils.ResultData;
import com.platform.qujia.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;


/**
 * 双屏机非油订单支付
 * */
@RestController
@RequestMapping
@Slf4j
public class DualNonOilOrderPayController {

    @Autowired
    private DualNonOilOrderPayService dualNonOilOrderPayService;

    /**
     * 双屏机非油订单  B扫C支付
     * */
    @ApiOperation(value = "双屏机B扫C支付")
    @PostMapping("/dual/tradeReversePay")
    @ResponseBody
    public ResultData dualTradeReversePay(@RequestBody NonOilOrder nonOilOrder) {
        if(Objects.isNull(nonOilOrder) || Objects.isNull(nonOilOrder.getActuallyAmount())
                || StringUtils.isEmpty(nonOilOrder.getAuthCode()) || StringUtils.isEmpty(nonOilOrder.getTrmIp())){
            return ResultData.error("请求参数异常");
        }

        if(Objects.isNull(nonOilOrder.getStationId())){
            return ResultData.error("没有油站");
        }

        ScanOrderDTO scanOrderDTO = dualNonOilOrderPayService.dualScanPay(nonOilOrder);
        TianQuetechPayEnum status = scanOrderDTO.getStatus();
        if (status == TianQuetechPayEnum.PAY_SUCCESS) {
            return ResultData.success(scanOrderDTO);
        }else if(status == TianQuetechPayEnum.PAY_PAYING){
            return ResultData.error(CodeMsg.TRADE_REVERSE_PAY_WAIT_MINUTE.getRetCode(),CodeMsg.TRADE_REVERSE_PAY_WAIT_MINUTE.getMessage(),scanOrderDTO);
        }
        return ResultData.error(CodeMsg.PAY_FAIL.getRetCode(),CodeMsg.PAY_FAIL.getMessage(),scanOrderDTO);
    }


    /**
     * 双屏机B扫C创建非油订单
     *
     * */
    @ApiOperation(value = "双屏机B扫C创建非油订单")
    @PostMapping("/dual/createOrder")
    public ResultData createOrder(@RequestBody DualNonOilOrderPayListDto dualNonOilOrderPayListDto) {
        if(Objects.isNull(dualNonOilOrderPayListDto) || CollectionUtils.isEmpty(dualNonOilOrderPayListDto.getDualNonOilOrderPayDtoList())){
            return ResultData.error("没有购买商品");
        }

        if(Objects.isNull(dualNonOilOrderPayListDto.getStationId())){
            return ResultData.error("没有油站");
        }

        NonOilOrder order = dualNonOilOrderPayService.createOrder(dualNonOilOrderPayListDto);
        return ResultData.success(order);
    }

    /**
     * 双屏机B扫C创建非油订单随行付回调功能
     *
     * */
    @RequestMapping(value = "/getNonOilOrderCallback", consumes = "application/json", method = RequestMethod.POST)
    public ResultData getNonOilOrderCallback(@RequestBody Map<String,Object> request){
        log.info("=========================双屏机B扫C创建非油订单随行付回调功能===============================");
        log.info("双屏机B扫C创建非油订单随行付回调返回过来的参数："+request);
        return dualNonOilOrderPayService.callbackSpj(request);
    }

    /**
     * 双屏机B扫C创建非油订单查询
     * */


    /**
     * 双屏机非油订单现金下单结算
     * */
    @ApiOperation(value = "双屏机非油订单现金下单结算")
    @PostMapping("/dual/createOrderCash")
    public ResultData createOrderCash(@RequestBody DualNonOilOrderPayListDto dualNonOilOrderPayListDto) {
        if(Objects.isNull(dualNonOilOrderPayListDto) || CollectionUtils.isEmpty(dualNonOilOrderPayListDto.getDualNonOilOrderPayDtoList())){
            return ResultData.error("没有购买商品");
        }

        if(Objects.isNull(dualNonOilOrderPayListDto.getStationId())){
            return ResultData.error("没有油站");
        }

        NonOilOrder order = dualNonOilOrderPayService.createOrderCash(dualNonOilOrderPayListDto);
        return ResultData.success(order);
    }
}
